package common;

import gosnoopers.fr.HomePage;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;










import org.json.JSONObject;









import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Base64;
import android.util.Log;
import android.widget.Toast;

public class Common {
	
	// TOKEN
	public static String TOKEN_STRING = "YTExOWRlODliY2JiYWQ1Y2M4ODI2MDI4NzRmZDVmZWM1MmE3NzgxNzhhZTA0MDhiYmE3Yjc4YTVhYzFmMGIyYQ";
	public static String CAPCHA_STRING = "MDNlYzE5NTQ5OWUzZmFiNGM2ZTkzYmRhY2YyZmE4ZWY1MmE1N2RhYjAxYTA0ZTMzODdjMzA4NzhjYmRkNTZjYg";
	public static String MSG = "Please turn on wifi or 3G";
	// OWNER USER INFOMATION
	//public static UserLogin USER_INFOMATION;
	// This is used to load image normal in application
	
	public static boolean checkLogined = false;
	
    // The maximum number of item in once page for API
    public static final int LIMIT = 10;
    //Facebook
    public static JSONObject userInfo;
//    public static String APP_ID = "453345281375610";
    public static String AUTHORIZE_STRING="";
    public static String ID_NODE_STRING="key_node_id";
    
    public static int sizeScreenWidth=0;
    public static int sizeScreenHeight=0;
    
    public static String urlFormatImageUser;
    public static String urlFormatImageTopic;
    public static String urlFormatImageVideo;
    public static String[] arrFormatImageUser=null;
    public static String[] arrFormatImageTopic=null;
    public static String[] arrFormatImageVideo=null;
    public static String sizeAvatar49="49-49";
    public static String sizeAvatar150="150-150";
    
    
    public static String sizeVideo97="97-60";
    public static String sizeVideo100="100-65";

    public static String authorization(String str) {
        String base64EncodedCredentials = Base64.encodeToString(str.getBytes(),
                Base64.NO_WRAP);
        return base64EncodedCredentials;
    }
    
    public static void showToast(Toast toast) {
    	if (!toast.getView().isShown()) {
			toast.show();
		}
    }

    public static String encodingUTF8(String value) {
        String encodedUrl;
        try {
            encodedUrl = URLEncoder.encode(value, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            encodedUrl = value;
        }
        return encodedUrl;
    }

    public static String getUrlUserImageFill(String cdn, String imageName, String id) {
    	if (("".equals(cdn))
    			|| ("".equals(id))
    			|| ("".equals(imageName))) {
    		return "";
    	}
    	String imagePath = "/upload/user-photos/"+id+"/fill/120-120/"+imageName+"?album_id="+id;
    	/*StringBuilder imagePath = new StringBuilder(cdn);
    	imagePath.append("/upload/user-photos/");
    	imagePath.append(id);
    	imagePath.append("/fill/200-200/");
    	imagePath.append(imageName);
    	imagePath.append("?album_id=");
    	imagePath.append(id);*/
    	return imagePath;
    }

    public static String getUrlImageThumb(String cdn, String imageName, String albumId) {
    	if (("".equals(cdn))
    			|| ("".equals(imageName))
    			|| ("".equals(albumId))) {
    		return "";
    	}
    	StringBuffer imagePath = new StringBuffer(cdn);
		imagePath.append("/upload/gallery/thumbs/620-10000/");
//    	imagePath.append("/upload/gallery/thumbs/700-500/");
//    	imagePath.append("/upload/gallery/thumbs/250-250/");
    	imagePath.append(imageName);
    	imagePath.append("?album_id=");
    	imagePath.append(albumId);
    	return imagePath.toString();
    }
    
    public static String getUrlImageFill(String cdn, String imageName, String id) {
    	if (("".equals(cdn))
    			|| ("".equals(id))
    			|| ("".equals(imageName))) {
    		return "";
    	}
    	StringBuffer imagePath = new StringBuffer(cdn);
    	imagePath.append("/upload/gallery/fill/120-120/");
    	imagePath.append(imageName);
    	imagePath.append("?album_id=");
    	imagePath.append(id);
    	return imagePath.toString();
    }

    /**
    * if(type==2)//video se truyen id =""
    * 
    * @author: truong.le
    * @datetime: Dec 13, 2013 4:39:24 PM
    * @return: String
    * @param type -- 0 = user; 1 = topic; 2 = video
    * @param cdn
    * @param imageName
    * @param id
    * @param strSizeInput
    * @returnTODO
    * 
    */
    public static String getRealImageToFill(int type, String cdn, String imageName, String id, String strSizeInput)
    {
    	/*user: "upload/user-photos/{id}/fill/{width}-{height}/{image}?album_id={id}"
		topic: "upload/gallery/fill/{width}-{height}/{image}?album_id={id}"
		video: "upload/videos/fill/{width}-{height}/{thumbnail}"*/
		String urlReturn = ""+cdn;
		String strSize = "49-49";
		try {
			switch (type) {
			case 0:
				try {
					for (int i = 0; i < arrFormatImageUser.length; i++) {
						if(strSizeInput.equals(arrFormatImageUser[i]))
						{
							strSize = strSizeInput;
							break;
						}
							
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				break;
			case 1:
				try {
					for (int i = 0; i < arrFormatImageTopic.length; i++) {
						if(strSizeInput.equals(arrFormatImageTopic[i]))
						{
							strSize = strSizeInput;
							break;
						}
							
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				
				break;
			case 2:
				try {
					for (int i = 0; i < arrFormatImageVideo.length; i++) {
						if(strSizeInput.equals(arrFormatImageVideo[i]))
						{
							strSize = strSizeInput;
							break;
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				
				break;
			default:
				strSize = "49-49";
				break;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			switch (type) {
			case 0://user
				//"upload/user-photos/{id}/fill/{size}/{image}?album_id={id}"
				urlReturn = urlReturn + urlFormatImageUser;
				try {
					urlReturn = urlReturn.replace("{id}", id);
				} catch (Exception e) {
					e.printStackTrace();
				}
				try {
					urlReturn = urlReturn.replace("{size}", strSize);
				} catch (Exception e) {
					e.printStackTrace();
				}
				try {
					urlReturn = urlReturn.replace("{image}", imageName);
				} catch (Exception e) {
					e.printStackTrace();
				}
				break;
			case 1://topic
				//"upload/gallery/fill/{size}/{image}?album_id={id}"
				urlReturn = urlReturn + urlFormatImageTopic;
				try {
					urlReturn = urlReturn.replace("{id}", id);
				} catch (NullPointerException e) {
					return null;
				}
				urlReturn = urlReturn.replace("{size}", strSize);
				try {
					urlReturn = urlReturn.replace("{image}", imageName);
				} catch (NullPointerException e) {
					return null;
				}
				
				break;
			case 2://video
				//"upload/videos/fill/{size}/{thumbnail}"
				urlReturn = urlReturn + urlFormatImageVideo ;
				urlReturn = urlReturn.replace("{size}", strSize);
				urlReturn = urlReturn.replace("{thumbnail}", imageName);
				break;
			case 3://photo
				
				break;
			default:
				break;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("urlReturn:"+urlReturn);
		return urlReturn;
    }
    public static String encryptMD5(String input) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] messageDigest = md.digest(input.getBytes());
            BigInteger number = new BigInteger(1, messageDigest);
            String hashtext = number.toString(16);
            while (hashtext.length() < 32) {
                hashtext = "0" + hashtext;
            }
            return hashtext;
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }
    
	
    
}
